package com.ev.common.jobs;

import com.ev.asl.constant.AslConstant;
import com.ev.asl.domain.AslBarCodeDO;
import com.ev.asl.dto.AslBarCodeParam;
import com.ev.asl.service.AslBarCodeService;
import com.ev.custom.domain.MaterielDO;
import com.ev.custom.service.MaterielService;
import com.ev.custom.service.PatrolPlanService;
import com.ev.framework.config.ApplicationContextRegister;
import com.ev.mes.domain.ProductionPlanDO;
import com.ev.mes.service.ProductionPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *条码自动绑定生产计划
 */
@Component
@Slf4j
public class CodeBindingPlanJob {
    @Scheduled(cron = "0 10 23 * * ?")
    public void executeInternal(){
        try {
            log.info("222222222222222222");
            excuteNotice();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    private void  excuteNotice() throws ParseException{
        AslBarCodeService aslBarCodeService = ApplicationContextRegister.getBean(AslBarCodeService.class);
        ProductionPlanService productionPlanService = ApplicationContextRegister.getBean(ProductionPlanService.class);
        MaterielService materielService = ApplicationContextRegister.getBean(MaterielService.class);
        //未绑定生产计划的条码
        List<AslBarCodeDO> aslBarCodeDOS = aslBarCodeService.listJob();
        log.info("aslBarCodeDOS:"+aslBarCodeDOS);
        if(aslBarCodeDOS.size()== AslConstant.ZERO){
            return;
        }
        Map<String,Object> params = new HashMap<>();
        //所有生产计划集合
        List<ProductionPlanDO> productionPlanDOS = productionPlanService.listProduction(params);
        //需要修改生产计划集合
        List<ProductionPlanDO> saveVo = new ArrayList<>();
        Map<String, ProductionPlanDO> collect = productionPlanDOS.stream().collect(Collectors.toMap(ProductionPlanDO::getMaterielSpecification, Function.identity(), (oldValue, newValue) -> newValue));
        log.info("collect:"+collect);
        for (AslBarCodeDO bar : aslBarCodeDOS) {
            ProductionPlanDO planDO = collect.get(bar.getProductModel());
            if(ObjectUtils.isEmpty(planDO)){
                continue;
            }
            MaterielDO materielDO = materielService.get(planDO.getMaterielId());
            bar.setUuid(planDO.getId());
            bar.setProductionPlanNo(planDO.getPlanNo());
            bar.setProductId(materielDO.getId());
            bar.setProductCode(materielDO.getSerialNo());
            bar.setProductModel(materielDO.getSpecification());
            bar.setBagging(1);
            bar.setTrayId(0l);
            bar.setScrapStatus(0);
            bar.setDelFlag(0);
            BigDecimal lotNoCount = ObjectUtils.isEmpty(planDO.getLotNoCount()) ? BigDecimal.ZERO : planDO.getLotNoCount();
            planDO.setLotNoCount(lotNoCount.add(new BigDecimal(AslConstant.ONE)));
            saveVo.add(planDO);
            aslBarCodeService.update(bar);
        }
        log.info(saveVo.toString());
//        aslBarCodeService.batchUpdate(aslBarCodeDOS);
        if(saveVo.size()!=AslConstant.ZERO){
            productionPlanService.batchUpdateForLotNo(saveVo);
        }
    }
}